{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gensim\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from chatbot.corpus import XhjCorpus as Corpus\n",
    "from chatbot.vector import WordVec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache /tmp/jieba.cache\n",
      "Loading model cost 0.853 seconds.\n",
      "Prefix dict has been built succesfully.\n"
     ]
    }
   ],
   "source": [
    "corpus = Corpus()\n",
    "wv = WordVec()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = list(set(corpus.quess))\n",
    "feats = wv.docs2vec(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,\n",
       "    n_clusters=15, n_init=10, n_jobs=None, precompute_distances='auto',\n",
       "    random_state=None, tol=0.0001, verbose=0)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "km = KMeans(n_clusters=15)\n",
    "km.fit(feats)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32035"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred = km.predict(feats)\n",
    "rs = []\n",
    "for i, y in enumerate(y_pred):\n",
    "    if y == 5:\n",
    "        rs.append(corpus.quess[i])\n",
    "len(rs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('你',),\n",
       " ('你', '是', '一只', '贱', '贱', '的', '鸡'),\n",
       " ('白富', '美'),\n",
       " ('夸下', '我帅'),\n",
       " ('坨', '坨'),\n",
       " ('这', '我', '知道', '…', '…', '吼吼'),\n",
       " ('谁',),\n",
       " ('说', '你'),\n",
       " ('烤鸡',),\n",
       " ('他', '说', '他', '喜欢', '你'),\n",
       " ('嘿嘿',),\n",
       " ('嘿嘿',),\n",
       " ('反正', '没想', '你'),\n",
       " ('你', '是', '公鸡', '还是', '母鸡'),\n",
       " ('噗',),\n",
       " ('傻', '逼', '鸡'),\n",
       " ('走', '你'),\n",
       " ('从来', '都', '不会', '有人', '爱', '我'),\n",
       " ('米', '兔'),\n",
       " ('在', '周末', '晚上'),\n",
       " ('韩剧', '美剧'),\n",
       " ('不',),\n",
       " ('傻',),\n",
       " ('蚯蚓', '吃', '吗'),\n",
       " ('给', '你', '大', '蟒蛇', '你', '吃', '吗'),\n",
       " ('再', '后来'),\n",
       " ('习总',),\n",
       " ('苹果',),\n",
       " ('我', '是', '谁'),\n",
       " ('笑话',)]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rs[:30]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.externals import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['chatbot/models/xhjkm.m']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(km, \"chatbot/models/xhjkm.m\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "ques = corpus.quess\n",
    "answer = corpus.answers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(ques) == len(answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "cp = (ques, answer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"chatbot/data/xhj.json\", 'w') as f:\n",
    "    json.dump(cp,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"chatbot/data/xhj.json\") as f:\n",
    "    data2 = json.load(f)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
